let behaviorsGetLocation = require('../../behaviors/getLocation.js')
const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
  showMsg
} from '../../../../utils/config.js'
import {
  reqConsumerHomeUpdate,
  reqConsumerHomeDel,
  reqUpdate,
  reqRemove
} from '../../../../request/api.js';
Page({
  behaviors: [behaviorsGetLocation],
  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离

    form: {
      homeName: '',
      lng: '',
      lat: '',
      address: '',
      rooms: [],
      roomCount: 0,
      avatar:"",
      showRoleSelection:false,
    },

    showInputDialog: false,
    memberInfo:{},
    roles:"",//1家庭创建者 2管理员 3普通成员
    mobile:"",
  },

  onLoad(options) {
    console.log(JSON.parse(options.memberInfo),'options');
    // this.getParams()
    this.setData({
      memberInfo:JSON.parse(options.memberInfo),
      roles:JSON.parse(options.roles),
      mobile:app.globalData.mobile
    })
  },

  onShow() {

  },

  onChangeRoom() {
    // if(this.data.memberInfo.role==1 || this.data.memberInfo.mobile==this.data.mobile || this.data.roles==3 || this.data.roles==this.data.memberInfo.role) return
    if( this.data.roles!=1 || this.data.memberInfo.role==1) return
    this.setData({
      showRoleSelection: true
    })
  },

  getParams() {
    const _this = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('sendDataToDetail', function (res) {
      _this.setData({
        form: res.data
      })
      console.log('form', _this.data.form);
    })
  },

  goRoom() {
    const _this = this
    wx.navigateTo({
      url: '/packageC/pages/room/room/room',
      events: {
        receiveDataFromRoom: function (res) {
          _this.setData({
            "form.roomCount": res.data
          });
          _this.notifyPageUpdataFoo()
        },
      },
      success: function (res) {
        res.eventChannel.emit('sendDataToRoom', {
          data: _this.data.form
        })
      }
    })
  },


  onName() {
    if(this.data.roles==1 ||(this.data.roles==2 && this.data.memberInfo.mobile==this.data.mobile)) {
      this.setData({
        showInputDialog: true
      })
    }
  },

  async inputDialogHandle(e) {
    console.log(e);

    const {
      actionType,
      value
    } = e.detail
    if (actionType === 'confirm') {
      if (!value) return showMsg("请填写家庭名称")
      const res = await reqUpdate({
        homeConsumerId:this.data.memberInfo.id,
        // userId:this.data.memberInfo.userId,
        userId: app.globalData.userId,
        remark:value,
        role:this.data.memberInfo.role,
      })
      if (res.code != 20000) return showMsg(res.message);
      this.setData({
        showInputDialog: false,
        'memberInfo.remark':value
      })
    } else {
      this.setData({
        showInputDialog: false
      })
    }
  },

  getLocation(location) {
    const {
      longitude,
      latitude,
      province,
      city,
      district,
      name
    } = location
    console.log(longitude,
      latitude, );

    this.setData({
      'from.lng': longitude,
      'form.lat': latitude,
      'form.address': name,
    })

    this.reqConsumerHomeUpdateFoo()
  },

  onDel() {
    Dialog.confirm({
        title: '确定要移除成员吗？',
        message: '移除后，该成员将不能查看和操作家庭里面的设备',
      })
      .then(async () => {
        const res = await reqRemove({
          homeConsumerId:this.data.memberInfo.id,
          userId: app.globalData.userId,
        })
        if (res.code != 20000) return showMsg(res.message);
        showMsg("移除成功")
        setTimeout(() => {
          wx.navigateBack()
        }, 500)
      })
      .catch(() => {});
  },

  async reqConsumerHomeUpdateFoo() {
    const {
      form
    } = this.data
    const res = await reqConsumerHomeUpdate({
      homeId: form.id,
      homeName: form.homeName,
      lng: form.lng,
      lat: form.lat,
      address: form.address,
    })
    if (res.code != 20000) return showMsg(res.message);
    showMsg("修改成功", 'success')

    let {
      familyInfo
    } = app.globalData
    if (familyInfo.id === form.id) {
      familyInfo.homeName = form.homeName
      app.globalData.familyInfo = familyInfo
      wx.setStorageSync('familyInfo', familyInfo);
    }

    setTimeout(_ => {
      this.notifyPageUpdataFoo()
    }, 1000)
  },

  notifyPageUpdataFoo() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('receiveDataFromDetail', {
      isUpdataList: true
    });
  },
  async confirmRoomSelection(e) {
    const {
      action,
      roomInfo
    } = e.detail
    console.log(roomInfo,'e');
    if (action == 'confirm') {
      const res = await reqUpdate({
        homeConsumerId:this.data.memberInfo.id,
        userId: app.globalData.userId,
        remark:this.data.memberInfo.remark,
        role:roomInfo.id,
      })
      if (res.code != 20000) return showMsg(res.message);
      this.setData({
        showRoleSelection: false,
        'memberInfo.role':roomInfo.id
      })
    } else {
      this.setData({
        showRoleSelection: false
      })
    }
  },


})